﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace Avivo.Tools.Sprite
{
	public partial class SpriteItem
	{
		#region Public properties
		public string Name
		{
			get;
			set;
		}

		public string Text
		{
			get;
			set;
		}

		//Alternative to Text, when using more than one constant in xaml
		public Dictionary<string, string> Messages
		{
			get;
			set;
		}

		public string Style
		{
			get;
			set;
		}

		public string Source
		{
			get;
			set;
		}

		public string Target
		{
			get;
			set;
		}


		public int Width
		{
			get;
			set;
		}

		public int Height
		{
			get;
			set;
		}

		public int X
		{
			get;
			set;
		}

		public int Y
		{
			get;
			set;
		}
		#endregion

		#region Serialization
		internal static SpriteItem Create(XElement element)
		{
			SpriteItem result = null;
			if (element.Name == "SpriteItem")
			{
				result = new SpriteItem();

				result.Name = (element.Attribute("Name") != null ? element.Attribute("Name").Value : null);
				result.Source = (element.Attribute("Source") != null ? element.Attribute("Source").Value : null);
				result.Target = (element.Attribute("Target") != null ? element.Attribute("Target").Value : null);

				int width = 0;
				int.TryParse(element.Attribute("Width") != null ? element.Attribute("Width").Value : null, out width);
				result.Width = width;

				int height = 0;
				int.TryParse(element.Attribute("Height") != null ? element.Attribute("Height").Value : null, out height);
				result.Height = height;

				int x = 0;
				int.TryParse(element.Attribute("X") != null ? element.Attribute("X").Value : null, out x);
				result.X = x;

				int y = 0;
				int.TryParse(element.Attribute("Y") != null ? element.Attribute("Y").Value : null, out y);
				result.Y = y;

				int count = 0;
				var messages = element.Elements("Label");
				result.Messages = new Dictionary<string, string>();
				foreach (var message in messages)
				{
					string key = (message.Attribute("Key") != null ? message.Attribute("Key").Value : null);
					string value = (message.Attribute("Value") != null ? message.Attribute("Value").Value : null);
					result.Messages.Add(key, value);
					count++;
				}
				if (count == 0)
				{
					result.Messages = null;
					result.Text = (element.Attribute("Text") != null ? element.Attribute("Text").Value : null);
				}
			}
			return result;
		}

		internal XElement ToXElement()
		{
			XElement element = new XElement("SpriteItem");
			if (this.Name != null)
			{
				element.Add(new XAttribute("Name", this.Name));
			}
			if (this.Source != null)
			{
				element.Add(new XAttribute("Source", this.Source));
			}
			if (this.Target != null)
			{
				element.Add(new XAttribute("Target", this.Target));
			}
			if (this.Width > 0)
			{
				element.Add(new XAttribute("Width", this.Width));
			}
			if (this.Height > 0)
			{
				element.Add(new XAttribute("Height", this.Height));
			}
			if (this.X > 0)
			{
				element.Add(new XAttribute("X", this.X));
			}
			if (this.Y > 0)
			{
				element.Add(new XAttribute("Y", this.Y));
			}

			if (this.Messages != null && this.Messages.Count > 0)
			{
				foreach (var pair in this.Messages)
				{
					element.Add(
						new XElement("Label",
							new XAttribute("Key", pair.Key),
							new XAttribute("Value", pair.Value)));
				}
			}
			else if (this.Text != null)
			{
				element.Add(new XAttribute("Text", this.Text));
			}

			return element;
		}
		#endregion
	}
}
